package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.LiveListBeen;
import cn.xfdzx.android.apps.shop.bean.SubmitWithdrawalBeen;
import cn.xfdzx.android.apps.shop.bean.WithdrawalListBeen;
import cn.xfdzx.android.apps.shop.contract.LiveListContract;
import cn.xfdzx.android.apps.shop.contract.WithdrawalListContract;
import cn.xfdzx.android.apps.shop.model.LiveListModel;
import cn.xfdzx.android.apps.shop.model.WithdrawalListModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class WithdrawalListPresenter extends BasePresenter<WithdrawalListContract.View> implements WithdrawalListContract.Presenter {

    private WithdrawalListContract.Model model;

    public WithdrawalListPresenter() {
        model = new WithdrawalListModel();
    }


    @Override
    public void withdrawalList(boolean isRefresh, boolean isLoading, String storeId, String page, String limit) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading) {
            mView.showLoading();
        }
        model.withdrawalList(storeId,page, limit)
                .compose(RxScheduler.<WithdrawalListBeen>Flo_io_main())
                .as(mView.<WithdrawalListBeen>bindAutoDispose())
                .subscribe(new Consumer<WithdrawalListBeen>() {
                    @Override
                    public void accept(WithdrawalListBeen bean) throws Exception {
                        mView.onWithdrawalListSuccess(bean);
                        if (isLoading) {
                            mView.hideLoading();
                        }
                        if (isRefresh) {
                            mView.finishRefresh();
                        } else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading) {
                            mView.hideLoading();
                        }
                        if (isRefresh) {
                            mView.finishRefresh();
                        } else {
                            mView.finishLoadMore();
                        }
                    }
                });

    }

    @Override
    public void submitWithdrawal(String store_id, String is_full, String drawing_flag, String order_sn, String remark) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.submitWithdrawal(store_id, is_full,drawing_flag,order_sn,remark)
                .compose(RxScheduler.<SubmitWithdrawalBeen>Flo_io_main())
                .as(mView.<SubmitWithdrawalBeen>bindAutoDispose())
                .subscribe(new Consumer<SubmitWithdrawalBeen>() {
                    @Override
                    public void accept(SubmitWithdrawalBeen bean) throws Exception {
                        mView.onSubmitWithdrawalSuccess(bean);
                        mView.hideLoading();

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();

                    }
                });
    }


}
